import android.app.Activity
import android.app.Dialog
import android.app.SearchManager
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast
import com.example.timeseletor.R
import com.example.timeseletor.kotlin.TimeSelectorView
import java.io.Serializable

/**
 * @Author: whz
 * @Description:
 * @Date: create in 2021/4/24 17:24
 */
class TimePicker {
    private var text: String = ""
    private var mActivity: Activity? = null
    private var tvCancel: TextView? = null
    private var tvComFirm: TextView? = null
    private var tvTitle: TextView? = null
    private var yearSelector: TimeSelectorView? = null
    private var monthSelector: TimeSelectorView? = null
    private var daySelector: TimeSelectorView? = null
    private var mDialog: Dialog? = null
    private var mCancelListener: OnCancelListener? = null
    private var mComfirmListener: OnComfirmClickListener? = null
    private var yearSelecedNum: Int? = null
    private var monthSelecedNum: Int? = null
    private var daySelecedNum: Int? = null

    constructor(activity: Activity) {
        mActivity = activity
        initDialog()
    }

    private fun initDialog() {
        mDialog = Dialog(mActivity!!, R.style.time_dialog)
        mDialog!!.setContentView(
            mActivity!!.layoutInflater.inflate(
                R.layout.layout_time_selector,
                null
            )
        )
        var dd = mActivity!!.windowManager.defaultDisplay
        var dm = DisplayMetrics()
        dd.getMetrics(dm)
        var attrbutes = mDialog!!.window!!.attributes
        mDialog!!.window!!.setGravity(Gravity.BOTTOM)
        attrbutes.height = (dm.heightPixels * 0.4).toInt()
        attrbutes.width = dm.widthPixels
        mDialog!!.window!!.setWindowAnimations(R.style.dialogWindowAnimation)

        tvCancel = mDialog!!.findViewById(R.id.tvCancel)
        tvComFirm = mDialog!!.findViewById(R.id.tvConfirm)
        tvTitle = mDialog!!.findViewById(R.id.tvTitle)
        yearSelector = TimeSelectorView(mActivity!!, null, 0)
        yearSelector = mDialog!!.findViewById(R.id.yearPickView)
        yearSelecedNum = yearSelector!!.getCurrentPosition()
        setListener()
    }

    private fun setListener() {
        yearSelector!!.setOnSelectNumListener(object : TimeSelectorView.OnSelectNumListener {
            override fun onSelected(num: Int) {
                Log.e("sss", num.toString())
                yearSelecedNum = num
            }
        })
        tvComFirm!!.setOnClickListener {
            if (mComfirmListener != null) {
                mComfirmListener!!.onClick(yearSelecedNum!!)
            }
        }

        tvCancel!!.setOnClickListener {
            if (mCancelListener != null) {
                mCancelListener!!.onClick()
            }
        }
    }

    fun show() {
        if (mDialog != null) {
            mDialog!!.show()
        }
    }

    //    设置默认值
    fun setDefaultAttribute(isShowText:Boolean) {
        yearSelector!!.setDefaultAttribute("年",isShowText,2030,2021)
    }

    fun selecNum(num: Int) {
        yearSelector!!.select(num)
    }

    fun setOnCancelListener(listener: OnCancelListener) {
        this.mCancelListener = listener
    }

    fun setOnComfirmListener(listener: OnComfirmClickListener) {
        this.mComfirmListener = listener
    }

    fun setTitle(title: String) {
        tvTitle!!.setText(title)
    }

    interface OnCancelListener {
        fun onClick()
    }

    interface OnComfirmClickListener {
        fun onClick(num: Int)
    }

    interface OnNumChangeClickListener{
        fun onNumChange(num: Int)
    }

}